package com.yueman.ptoolopenticket.ticket.TicketService.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import com.yueman.ptoolopenticket.ticket.TicketService.CommonFileService;
import com.yueman.ptoolopenticket.ticket.Tpl.CommonFileTemplate;
import com.yueman.ptoolopenticket.ticket.aop.CommSignCheck;
import com.yueman.ptoolopenticket.ticket.dto.Result;
import com.yueman.ptoolopenticket.ticket.dto.ResultBuilder;
import com.yueman.ptoolopenticket.ticket.dto.request.lixin.RealNameAuthReq;
import com.yueman.ptoolopenticket.ticket.dto.response.lixin.RealNameAuthRes;
import com.yueman.ptoolopenticket.ticket.exception.RRException;
import com.yueman.ptoolopenticket.ticket.mapper.TicketUserRepository;
import com.yueman.ptoolopenticket.ticket.pojo.User;
import com.yueman.ptoolopenticket.ticket.utils.Constant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CommSignCheck
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CommonFileServiceimpl implements CommonFileService {
    public final Map<String, CommonFileTemplate> map;

    public final TicketUserRepository ticketUserRepository;

    /**
     * 实名认证
     * @param realNameAuthReq
     * @return
     */
    @Override
    public Result<RealNameAuthRes> rnameAuth(RealNameAuthReq realNameAuthReq){
        Result<RealNameAuthRes> realNameAuthQueryResResult=null;
        try {
            Map<String,MultipartFile>  imgmap=new HashMap<>();
            imgmap.put(realNameAuthReq.getName()+"face",realNameAuthReq.getFaceUrl());
            imgmap.put(realNameAuthReq.getName()+"Front",realNameAuthReq.getIDCardFrontUrl());
            imgmap.put(realNameAuthReq.getName()+"reverse",realNameAuthReq.getIDCardReverseUrl());
            SaveImg(imgmap);
            User user = ticketUserRepository.findUserByIdno(realNameAuthReq.getIDCardNo());
            if (user != null) {
                RealNameAuthRes realNameAuthRes = new RealNameAuthRes();
                realNameAuthRes.setRetcode(Constant.SUCCESS_CODE);
                realNameAuthRes.setRetMsg(Constant.AUTH_SUCCESS);
                return ResultBuilder.success(realNameAuthRes);
            }
            realNameAuthQueryResResult=map.get(realNameAuthReq.getChannel()).rnameAuth(realNameAuthReq);
        }catch (IOException e){
            throw   new RRException(e.getMessage());
        }
        return realNameAuthQueryResResult;
    }

    /**
     * 将图片保存到img文件中
     * @param map
     */
    public void SaveImg(Map<String,MultipartFile> map){
        try {
            for (Map.Entry<String, MultipartFile> entry : map.entrySet()) {
                MultipartFile file = entry.getValue();
                String AllName = file.getOriginalFilename();
                int length = AllName.lastIndexOf('.');
                String suffixName = AllName.substring(length);
                String path = System.getProperty("user.dir") + "\\img\\" +Base64.encode(entry.getKey()) + suffixName;
                log.info("path:{}", path);
                File sytfile = FileUtil.newFile(path);
                file.transferTo(sytfile);
                log.info("path:{}", path);
            }
        }catch (Exception e){
            throw new RRException(e.getMessage());
        }
    }
}
